package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Subject;
import cn.wolfcode.luowowo.article.domain.Ticket;
import cn.wolfcode.luowowo.article.domain.Tourist;
import cn.wolfcode.luowowo.ticket.service.ISubjectService;
import cn.wolfcode.luowowo.ticket.service.ITicketService;
import cn.wolfcode.luowowo.ticket.service.ITouristService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("ticket")
public class TicketController {
    @Reference
    private ISubjectService subjectService;

    @Reference
    private ITouristService touristService;

    @Reference
    private ITicketService ticketService;

    @RequestMapping("")
    public String index(Model model) {
        List<Subject> subList = subjectService.getListByDestId (3261L);

        Long subId = subList.get (0).getId ();
        List<Tourist> touristList = touristService.selectByDestIdAndSubId (3261L, subId);
        model.addAttribute ("subList", subList);
        model.addAttribute ("touristList", touristList);

        return "ticket/index";
    }

    @RequestMapping("detail")
    public String detail(Model model, Long id) {
        Tourist tourist = touristService.getById (id);


        List<Ticket> ticketList = ticketService.getByTouristId (id);

        List<Tourist> touristList = touristService.getCommendByTicketId(id);
        model.addAttribute ("tourist", tourist);
        model.addAttribute ("ticketList", ticketList);
        model.addAttribute ("touristList", touristList);
        return "ticket/detail";
    }

    @RequestMapping("subjectList")
    public String subjectList(Model model, Long ajaxDestId) {
        if (ajaxDestId == null) {
            ajaxDestId = 3261L;
        }
        List<Subject> subList = subjectService.getListByDestId (ajaxDestId);

        Long subId = subList.get (0).getId ();
        List<Tourist> touristList = touristService.selectByDestIdAndSubId (ajaxDestId, subId);
        model.addAttribute ("subList", subList);
        model.addAttribute ("touristList", touristList);

        return "ticket/touristTpl";
    }

    @RequestMapping("touristList")
    public String touristList(Model model, Long ajaxDestId, Long ajaxSubjectId) {

        List<Tourist> touristList = touristService.selectByDestIdAndSubId (ajaxDestId, ajaxSubjectId);
        model.addAttribute ("touristList", touristList);
        return "ticket/tourist2Tpl";
    }

    @RequestMapping("addOrder")
    public String addOrder(Model model, Long tid) {

        Ticket ticket = ticketService.getById (tid);
        model.addAttribute ("ticket", ticket);
        return "ticket/addOrder";
    }

}
